package video.com.controller;


import com.Exception.GlobalException;
import com.responseData.R;
import com.responseData.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import video.com.entity.ChatRecord;
import video.com.service.ChatRecordService;
import video.com.transportEntity.vo.CommentPage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwj
 * @since 2023-11-11
 */
@RestController
@RequestMapping("/video/chatRecord")
public class ChatRecordController {

    @Autowired
    private ChatRecordService chatRecordService;

    /**
     * 发表评论或回复评论
     * @param chatRecord
     * @return
     */
    @PostMapping("/sendComment")
    public R sendComment(@RequestBody ChatRecord chatRecord){

        String chatId = null;

        try{
            chatId = chatRecordService.sendComment(chatRecord);
        }catch (Exception e){
            if(e instanceof GlobalException){
                R r = ((GlobalException) e).getR();
                return r;
            }else{
                return ResponseData.error("异常错误");
            }
        }

        return ResponseData.success(chatId);
    }

    /**
     * 获取videoId的评论
     * @param info
     * @return
     */
    @PostMapping("/getCommentList")
    public R getCommentList( @RequestBody CommentPage info){
        List<Map<String, Object>> commendList = new ArrayList<>();
        try{
            commendList = chatRecordService.getCommentList(info);
        }catch (Exception e){
            if(e instanceof GlobalException){
                R r = ((GlobalException) e).getR();
                return r;
            }else{
                return ResponseData.error("异常错误");
            }
        }

        return ResponseData.success(commendList);
    }


    /**
     * 获取评论的回复楼层
     * @param info
     * @return
     */
    @PostMapping("/getSubCommentList")
    public R getSubCommentList(@RequestBody CommentPage info){
        try {
            info = chatRecordService.getSubCommentList(info);
        }catch (Exception e){
            if(e instanceof GlobalException){
                R r = ((GlobalException) e).getR();
                return r;
            }else{
                return ResponseData.error("异常错误");
            }
        }

        return ResponseData.success(info);
    }

    /**
     * 赞同评论
     * @param info
     * @return
     */
    @PostMapping("/agree")
    public R agree(@RequestBody Map<String, Object> info){
        try{
            chatRecordService.agree(info);
        }catch (Exception e){
            if(e instanceof GlobalException){
                R r = ((GlobalException) e).getR();
                return r;
            }else{
                return ResponseData.error("异常错误");
            }
        }

        return ResponseData.success("");
    }

    /**
     * 对评论点踩
     * @param info
     * @return
     */
    @PostMapping("/disagree")
    public R disagree(@RequestBody Map<String ,Object> info){
        try{
            chatRecordService.disagree(info);
        }catch (Exception e){
            if(e instanceof GlobalException){
                R r = ((GlobalException) e).getR();
                return r;
            }else{
                return ResponseData.error("异常错误");
            }
        }

        return ResponseData.success("");
    }
}

